<?php
	// Class: DBConnector
	// 
	// Helper class that creates a correct data source connection
	// with a single call.
	//
	// Author: Arttu Valo
	// Date: 9.10.2006
	
	error_reporting(E_ALL);
	
	// Include data source classes
	require_once("MSSQL.php");
	require_once("MySQL.php");
	require_once("SQLitePDO.php");
	
	class DBConnector
	{
		private $db;
		private $type;
		
		const DBC_MSSQL = 1;
		const DBC_MYSQL = 2;
		const DBC_SQLITE = 3;
		
		// Constructor		
		public function DBConnector($type, $serverName, $userName, $passWord, $dbName="")
		{
			switch($type)
			{
				case DBConnector::DBC_MSSQL:
					$this->db = new MSSQL($serverName, $userName, $passWord, $dbName);
					break;
				case DBConnector::DBC_MYSQL:
					$this->db = new MySQL($serverName, $userName, $passWord, $dbName);
					break;
				case DBConnector::DBC_SQLITE:
					$this->db = new SQLitePDO($serverName, $userName, $passWord, $dbName);
					break;
			}
			$this->type = $type;
		}
		
		// get the data source type		
		public function GetType()
		{
			return $this->type;
		}
		
		// pass the call to the actual data source class		
		public function __call($name, $args)
		{
			if(!strcmp($name, "Query"))
			{
				if(count($args) != 1)
				{
					return NULL;
				}
				return $this->db->Query($args[0]);
			}
			elseif(!strcmp($name, "SelectDB"))
			{
				if(count($args) != 1)
				{
					return NULL;
				}
				return $this->db->SelectDB($args[0]);
			}
			elseif(!strcmp($name, "Connect"))
			{
				if(count($args) == 3)
				{
					return $this->db->Connect($args[0], $args[1], $args[2]);
				}
				elseif(count($args) == 4)
				{
					return $this->db->Connect($args[0], $args[1], $args[2], $args[3]);
				}
				else
				{
					return NULL;
				}
			}
			elseif(!strcmp($name, "Close"))
			{
				return $this->db->Close();
			}
			elseif(!strcmp($name, "GetVersion"))
			{
				return $this->db->GetVersion();
			}
		}
	}
?>